package com.school.model.dao;

// default package
// Generated Sep 24, 2013 9:15:18 AM by Hibernate Tools 3.4.0.CR1

import javax.ejb.Stateless;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.school.model.SchoolSessionFactory;
import com.school.model.entity.Klasse;

/**
 * Home object for domain model class Klasse.
 * 
 * @see .Klasse
 * @author Hibernate Tools
 */
@Stateless
public class KlasseHome {

	private static final Log log = LogFactory.getLog(KlasseHome.class);

	private SessionFactory entityManager;

	public KlasseHome() {
		entityManager = SchoolSessionFactory.getInstance();
	}

	public void persist(Klasse transientInstance) {
		log.debug("persisting Klasse instance");
		Session session = null;
		Transaction transaction = null;
		try {
			session = entityManager.openSession();
			transaction = session.beginTransaction();
			session.save(transientInstance);
			transaction.commit();
			session.close();
			log.debug("persist of Klasse " + transientInstance.getName()
					+ " successful");
		} catch (HibernateException re) {
			log.error("persist of Klasse failed", re);
			throw re;
		} finally {
			if (transaction != null && transaction.isActive()) {
				transaction.rollback();
			}
			if (session != null && session.isOpen())
				session.close();
		}
	}

	public void remove(Klasse persistentInstance) {
		log.debug("removing Klasse instance");
		Session session = null;
		Transaction transaction = null;
		try {
			session = entityManager.openSession();
			transaction = session.beginTransaction();
			session.delete(persistentInstance);
			transaction.commit();
			session.close();
			log.debug("removing Klasse " + persistentInstance.getName()
					+ " successful");
		} catch (HibernateException re) {
			log.error("removing Klasse failed", re);
			throw re;
		} finally {
			if (transaction != null && transaction.isActive()) {
				transaction.rollback();
			}
			if (session != null && session.isOpen())
				session.close();
		}
	}

	public Klasse merge(Klasse detachedInstance) {
		log.debug("merging Klasse instance");
		Session session = null;
		Transaction transaction = null;
		Klasse mergedKlasse = null;
		try {
			session = entityManager.openSession();
			transaction = session.beginTransaction();
			mergedKlasse = (Klasse) session.merge(detachedInstance);
			transaction.commit();
			session.close();
			log.debug("merge successful. Following Klasse merged: "
					+ mergedKlasse.getName());
			return mergedKlasse;
		} catch (HibernateException re) {
			log.error("merge failed", re);
			throw re;
		} finally {
			if (transaction != null && transaction.isActive()) {
				transaction.rollback();
			}
			if (session != null && session.isOpen())
				session.close();
		}
	}

	public Klasse findById(int id) {
		log.debug("getting Klasse instance with id: " + id);
		Session session = null;
		Transaction transaction = null;
		Klasse klasseById = null;
		try {
			session = entityManager.openSession();
			transaction = session.beginTransaction();
			klasseById = (Klasse) session
					.createQuery("from Klasse as f where f.id = :id")
					.setInteger("id", id).list().get(0);
			transaction.commit();
			session.close();
			log.debug("find by id successful. Following Klasse found "
					+ klasseById.getName());
			return klasseById;
		} catch (HibernateException re) {
			log.error("find by id failed", re);
			throw re;
		} finally {
			if (transaction != null && transaction.isActive()) {
				transaction.rollback();
			}
			if (session != null && session.isOpen())
				session.close();
		}
	}
}
